package com.gviiii.pprf.common;

import static com.gviiii.pprf.common.RFLibrary.*;

public class Rf {

    public static String falsereason(String s) {
        String reason="";
        if (s.equals("")) {
            reason = "";
            return reason;
        }
        if (s.equals("00")) {
            reason = "命令执行成功 .....";
            return reason;
        }
        if (s.equals("1")) {
            reason = "命令操作失败 .....";
            return reason;
        }
        if (s.equals("2")) {
            reason = "地址校验错误 .....";
            return reason;
        }
        if (s.equals("3")) {
            reason = "找不到已选择的端口 .....";
            return reason;
        }
        if (s.equals("4")) {
            reason = "读写器返回超时 .....";
            return reason;
        }
        if (s.equals("5")) {
            reason = "数据包流水号不正确 .....";
            return reason;
        }
        if (s.equals("7")) {
            reason = "接收异常 .....";
            return reason;
        }
        if (s.equals("10")) {
            reason = "参数值超出范围 .....";
            return reason;
        }
        if (s.equals("80")) {
            reason = "参数设置成功 .....";
            return reason;
        }
        if (s.equals("81")) {
            reason = "参数设置失败 .....";
            return reason;
        }
        if (s.equals("82")) {
            reason = "通讯超时.....";
            return reason;
        }
        if (s.equals("83")) {
            reason = "卡不存在.....";
            return reason;
        }
        if (s.equals("84")) {
            reason = "接收卡数据出错.....";
            return reason;
        }
        if (s.equals("85")) {
            reason = "未知的错误.....";
            return reason;
        }
        if (s.equals("87")) {
            reason = "输入参数或者输入命令格式错误.....";
            return reason;
        }
        if (s.equals("89")) {
            reason = "输入的指令代码不存在.....";
            return reason;
        }
        if (s.equals("8A")) {
            reason = "在对于卡块初始化命令中出现错误.....";
            return reason;
        }
        if (s.equals("8B")) {
            reason = "在防冲突过程中得到错误的序列号.....";
            return reason;
        }
        if (s.equals("8C")) {
            reason = "密码认证没通过.....";
            return reason;
        }
        if (s.equals("8F")) {
            reason = "读取器接收到未知命令.....";
            return reason;
        }
        if (s.equals("90")) {
            reason = "卡不支持这个命令.....";
            return reason;
        }
        if (s.equals("91")) {
            reason = "命令格式有错误.....";
            return reason;
        }
        if (s.equals("92")) {
            reason = "在命令的FLAG参数中，不支持OPTION 模式.....";
            return reason;
        }
        if (s.equals("93")) {
            reason = "要操作的BLOCK不存在.....";
            return reason;
        }
        if (s.equals("94")) {
            reason = "要操作的对象已经别锁定，不能进行修改.....";
            return reason;
        }
        if (s.equals("95")) {
            reason = "锁定操作不成功.....";
            return reason;
        }
        if (s.equals("96")) {
            reason = "写操作不成功.....";
            return reason;
        }
        return reason;
    }

    /**
     * 获取卡号
     * @return
     */
    public static String getSnr() {
        int mode = 0x26;
        int halt = 0x00;
        byte[] snr = new byte[256];
        byte[] value = new byte[256];
        int nRet = INSTANTCE.MF_Getsnr(mode, halt, snr, value);
        if (0 == nRet) {
            return nRet+"|"+RFUtils.ByteArrayToStr(value, 0, 4);
        } else {
            return nRet+"|"+falsereason(RFUtils.ByteArrayToStr(snr, 0, 1));
        }
    }

    public static String getSerNum() {
        byte[] byteBuffer = new byte[256];
        int nRet = INSTANTCE.GetSerNum(byteBuffer);
        if (nRet == 0) {
            return nRet + "|" + RFUtils.ByteArrayToStr(byteBuffer, 1, 9);
        } else {
            return nRet + "|" + falsereason(RFUtils.ByteArrayToStr(byteBuffer, 0, 1));
        }
    }

    public static String setSerNum(String no) {
        byte[] byteBuffer = new byte[256];
        byte[] snr = RFUtils.hexStringToByte(no.replace(" ", ""));
        INSTANTCE.SetSerNum(snr, byteBuffer);
        String str = RFUtils.ByteArrayToStr(byteBuffer, 0, 1);
        if ("80".equals(str.trim())) {
            return "0|";
        } else {
            return "1|" + falsereason(RFUtils.ByteArrayToStr(byteBuffer, 0, 1));
        }
    }

    public static void main(String[] args) {
        System.out.println(getSnr());
        System.out.println(getSerNum());
        System.out.println(setSerNum("ffffffffffffffff"));
    }
}
